package com.net.dao.sell.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import com.net.dao.SuperDao;
import com.net.dao.sell.SellMainDao;
import com.net.model.SellMain;

@Component("sellMainDao")
public class SellMainDaoImpl extends SuperDao implements SellMainDao {

	public void save(SellMain sellMain) {
		getHibernateTemplate().save(sellMain);
	}

	public void update(SellMain sellMain) {
		getHibernateTemplate().saveOrUpdate(sellMain);
	}
	
	public boolean delete(SellMain sellMain) {
		SellMain sellmain = (SellMain) this.getHibernateTemplate().load(SellMain.class, sellMain.getId());
		if(sellmain!=null){
			//sellmain.setResources(null);
			this.getHibernateTemplate().delete(sellmain);
			return true;
		}else
			return false;
	}

	@SuppressWarnings("unchecked")
	public int getCount() {
		HibernateCallback hc = new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery("from SellMain").list();
			}
			
		};
		return getHibernateTemplate().executeFind(hc).size();
	}

	@SuppressWarnings("unchecked")
	public List<SellMain> getSellMains(final int currentPage, final int perPage,
			final String orderType, final String orderName) {
		HibernateCallback hc = new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				if(orderType==null||orderName==null){
				return session.createQuery("from SellMain").setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
				}else{
					System.out.println("from SellMain order by "+orderName+" "+ orderType);
					return session.createQuery("from SellMain order by "+orderName+" "+ orderType).setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
				}
			}
		};
		
		System.out.println(getHibernateTemplate().executeFind(hc));
		return (List<SellMain>)getHibernateTemplate().execute(hc);
	}

	@SuppressWarnings("unchecked")
	public List<SellMain> getSellMainsByCondition(final int currentPage, final int perPage,
			final String condition) {
		HibernateCallback hc = new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String str = "from SellMain S where " + condition;
				System.out.println(str);
				return session.createQuery(str).setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
			}
			
		};
		return getHibernateTemplate().executeFind(hc);
	}

	public SellMain loadSellMainById(int id) {
		return (SellMain) this.getHibernateTemplate().load(SellMain.class, id);
	}

	@SuppressWarnings("unchecked")
	public SellMain loadSellMainByName(String name) {
		List<SellMain> sellMains = getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(SellMain.class).add(Restrictions.eq("name", name)));
		if(sellMains!=null&&sellMains.size()>0){
			return sellMains.get(0);
		}else
			return null;
	}
}
